package com.ruoyi.system.config;

import com.ruoyi.system.domain.vo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


@RestController
@RequestMapping("/minio")
public class MinioUploadController {
    /**
     * 简单工厂模式
     */
    private final MinioService minioService = new MinioService();

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("文件为空");
        }
        // 1. 保存到临时文件
        String tempPath = System.getProperty("java.io.tmpdir") + File.separator + file.getOriginalFilename();
        File tempFile = new File(tempPath);
        try {
            file.transferTo(tempFile);
            // 2. 上传到Minio（自动重命名）
            String url = minioService.uploadFile(tempFile.getAbsolutePath(), file.getOriginalFilename());
            // 3. 删除临时文件
            tempFile.delete();
            // 4. 返回图片URL
            System.err.println("图片是 : "+url);
            return Result.ok(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败: " + e.getMessage());
        }
    }
}
